package com.epfresh.supplier.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.epfresh.api.widget.list.LoadMoreListView;
import com.epfresh.supplier.entity.EvaluateSumEntity;
import com.epfresh.supplier.widget.*;
import com.daimajia.swipe.SwipeLayout;
import com.epfresh.supplier.R;
import com.epfresh.supplier.delegate.SideBarDelegate;
import com.epfresh.supplier.entity.ContentEntity;
import com.epfresh.supplier.entity.CustomerItem;
import com.epfresh.supplier.entity.OrderUnClose;
import com.epfresh.supplier.entity.SortModel;
import com.epfresh.supplier.activity.*;
import com.epfresh.supplier.widget.BaseItemDecoration;
import com.epfresh.supplier.widget.CharacterParser;
import com.epfresh.supplier.widget.PinyinComparator;
import com.epfresh.supplier.widget.RecyclerViewAdapter;
import java.util.ArrayList;
import java.util.Collections;
import com.epfresh.supplier.adapter.*;
import com.epfresh.api.global.*;

/**
 * Created by zhuli on 2017/8/17.
 */

public class CustomerContactFragment extends BaseFragment implements ViewVariable, RecyclerViewAdapter.OnItemClickListener {
    View v;
    private RecyclerView mSortRv;
    private SideBarDelegate sideBar;
    private TextView dialog;
    private SortRecycleViewAdapter adapter;

    /**
     * 汉字转换成拼音的类
     */
    private CharacterParser characterParser;
    private ArrayList<SortModel> SourceDateList;

    /**
     * 根据拼音来排列ListView里面的数据类
     */
    private PinyinComparator pinyinComparator;
    private String mAction;
    private Context mContext;
    private boolean isUpdateAdapter = true;

    @Override
    public String getBaseTag() {
        return "CustomerContactFragment";
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_customer_contact, container, false);
        initFragmentView();
        return v;
    }

    public void setUpdateAdapter(boolean b) {
        isUpdateAdapter = b;
    }

    private void initFragmentView() {
        // 实例化汉字转拼音类
        characterParser = CharacterParser.getInstance();
        pinyinComparator = new PinyinComparator();
        sideBar = (SideBarDelegate) v.findViewById(R.id.sb_customer);
        dialog = (TextView) v.findViewById(R.id.tv_customer_choose);
        sideBar.setTextView(dialog);

        // 设置右侧触摸监听
        sideBar.setOnTouchingLetterChangedListener(new SideBarDelegate.OnTouchingLetterChangedListener() {

            @Override
            public void onTouchingLetterChanged(String s) {
                // 该字母首次出现的位置
                int supplierition = adapter.getPositionForSection(s.charAt(0));
                if (supplierition != -1) {
                    adapter.smoothMoveToPosition(mSortRv, supplierition);
                }

            }
        });

        mSortRv = (RecyclerView) v.findViewById(R.id.rv);
        mSortRv.setLayoutManager(new LinearLayoutManager(getContext()));
        //mSortRv.addItemDecoration(new BaseItemDecoration(getContext()));
        adapter = new SortRecycleViewAdapter(null);
        mSortRv.setAdapter(adapter);
    }


    public static CustomerContactFragment newInstance() {
        return new CustomerContactFragment();
    }

    //传入数据
    public void transferData(ArrayList<SortModel> contact, String action) {
        this.SourceDateList = contact;
        this.mAction = action;

        adapter.setAction(mAction);
        //默认是有开单权限
        adapter.setOrderPermission(true);//ApplicationHelper.getInstance().getMerchant().isOrder());
        adapter.setData(SourceDateList);
        adapter.setOnItemClickListener(this);
    }


    // 本地筛选 根据输入框中的值来过滤数据并更新RecycleView
    //@param filterStr

    private void filterData(String filterStr) {
        ArrayList<SortModel> filterDateList = new ArrayList<SortModel>();

        if (TextUtils.isEmpty(filterStr)) {
            filterDateList = SourceDateList;
        } else {
            filterDateList.clear();
            for (SortModel sortModel : SourceDateList) {

                String name = sortModel.getCustomerItem().getName();
                String tel = sortModel.getCustomerItem().getPhone();
                if (!TextUtils.isEmpty(name)) {
                    if (name.indexOf(filterStr.toString()) != -1 || characterParser.getSelling(name).startsWith(filterStr.toString()) || tel.contains(filterStr)) {
                        filterDateList.add(sortModel);
                    }
                }
            }
        }
        // 根据a-z进行排序
        Collections.sort(filterDateList, pinyinComparator);
        if (null != mContext) {
            ((CustomerInfoActivity) mContext).updateFilterUI(filterDateList);
        }
        if(isUpdateAdapter) {
            adapter.setData(filterDateList);
        }

    }


    public void transferFilterString(String s, Context context) {
        mContext = context;
        filterData(s);
    }


    @Override
    public void onItemClick(RecyclerView parent, View view, int supplierition, SwipeLayout swipeLayout) {
        switch (parent.getId()) {
            case R.id.rv:
                switch (view.getId()) {
                    case R.id.item_ll_contact:
                        Intent intent = null;
                        switch (mAction) {
                            case HOME_ACTIVITY:

                                intent = new Intent(getContext(), CustomerDetailActivity.class);
                                intent.setAction(CUSTOMER_INFO_ACTIVITY);
                                intent.putExtra("customerItem", adapter.getItem(supplierition).getCustomerItem());
                                intent.putExtra("enterCustomerActivity", mAction);
                                if (getActivity() != null && getActivity() instanceof RequestCustomerListActivity) {
                                    Log.e("groupSize_fragment",""+((RequestCustomerListActivity) getActivity()).groupSize);

                                    intent.putExtra("groupSize",((RequestCustomerListActivity) getActivity()).groupSize);
                                }
                                startActivity(intent);

                                break;
                        }
                }
        }
    }
}

